package com.xhwl.logistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.logistics.bo.VehicleCarBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.input.PageBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.CreateVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.DeleteVehicleCarBO;
import com.xhwl.logistics.bo.manipulate.UpdateVehicleCarBO;
import com.xhwl.logistics.converter.ToCarVO;
import com.xhwl.logistics.converter.ToPageBO;
import com.xhwl.logistics.converter.ToPageVO;
import com.xhwl.logistics.converter.mapstruct.VehicleCarSecondConverter;
import com.xhwl.logistics.dto.manipulate.VehicleCarCreateDTO;
import com.xhwl.logistics.dto.manipulate.VehicleCarDeleteDTO;
import com.xhwl.logistics.dto.manipulate.VehicleCarUpdateDTO;
import com.xhwl.logistics.dto.query.VehicleCarDTO;
import com.xhwl.logistics.entity.VehicleCar;
import com.xhwl.logistics.enums.VehicleParkingLotCarAuthEnum;
import com.xhwl.logistics.service.business.VehicleCarBusinessService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.service.impl.VehicleCarServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotCarAuthHandler;
import com.xhwl.logistics.vo.PageVO;
import com.xhwl.logistics.vo.VehicleCarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//@RestController
public class VehicleCarSecondController {

    @Autowired
    private VehicleCarSecondConverter vehicleCarSecondConverter;

    @Autowired
    private VehicleCarBusinessService vehicleCarBusinessService;

    @Autowired
    private ToPageBO toPageBO;

    @Autowired
    private ToPageVO toPageVO;

    @Autowired
    private ToCarVO toCarVO;

    @Autowired
    private QueryDeviceService queryDeviceService;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private VehicleCarServiceImpl vehicleCarService;

    @PostMapping("/car/page")
    public ResultJson<PageVO<VehicleCarVO>> query(@RequestBody VehicleCarDTO vehicleCarDTO) {
        QueryVehicleCarBO queryVehicleCarBO = vehicleCarSecondConverter.toQueryVehicleCarBO(vehicleCarDTO);
        PageBO pageBO = toPageBO.toPageBOForCar(vehicleCarDTO);

        IPage<VehicleCarBO> vehicleCarBOIPage = vehicleCarBusinessService.queryCars(queryVehicleCarBO, pageBO);

        List<VehicleCarVO> vehicleCarVOList = vehicleCarSecondConverter.vehicleCarBOList2VehicleCarVOList(vehicleCarBOIPage.getRecords());
        List<VehicleCarVO> vehicleCarVOListAfterCompatibility = toCarVO.toCompatibility(vehicleCarVOList);

        return ResultJson.successWithResult(toPageVO.toPageVO(vehicleCarBOIPage, vehicleCarVOListAfterCompatibility));
    }

    @GetMapping("/car/detail")
    public ResultJson<VehicleCarVO> detail(Integer id) {
        VehicleCarBO vehicleCarBO = vehicleCarBusinessService.detailCar(id);

        VehicleCarVO vehicleCarVO = vehicleCarSecondConverter.vehicleCarBO2VehicleCarVO(vehicleCarBO);
        VehicleCarVO vehicleCarVOAfterCompatibility = toCarVO.toCompatibility(vehicleCarVO);

        return ResultJson.successWithResult(vehicleCarVOAfterCompatibility);
    }

    @PostMapping("/car/add")
    public ResultJson<Void> add(@RequestBody VehicleCarCreateDTO vehicleCarCreateDTO, Account account) {
        CreateVehicleCarBO createVehicleCarBO = vehicleCarSecondConverter.toCreateVehicleCarBO(vehicleCarCreateDTO, account.getId(), account.getName());

        //对新增的车辆进行授权
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(vehicleCarCreateDTO.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();
        IVehicleParkingLotCarAuthHandler vehicleParkingLotCarAuthHandler = applicationContext.getBean(VehicleParkingLotCarAuthEnum.toVehicleParkingLotCarAuthEnum(productId).getHandlerName(), IVehicleParkingLotCarAuthHandler.class);
//        String carDataStr = JSONObject.toJSONString(createVehicleCarBO);
        vehicleParkingLotCarAuthHandler.addCar(vehicleIntegratedConfigBO,createVehicleCarBO);

        vehicleCarBusinessService.addCar(createVehicleCarBO);

        return ResultJson.successWithVoid();
    }

//    @PostMapping("/car/delete")
//    public ResultJson<Void> delete(@RequestBody VehicleCarDeleteDTO vehicleCarDeleteDTO, Account account) {
//        DeleteVehicleCarBO deleteVehicleCarBO = vehicleCarSecondConverter.toDeleteVehicleCarBO(vehicleCarDeleteDTO, account.getId(), account.getName());
//
//        vehicleCarBusinessService.deleteCar(deleteVehicleCarBO);
//
//        return ResultJson.successWithVoid();
//    }
    @PostMapping("/car/delete")   //兼容以前Post，但删除id参数放到查询参数中
    public ResultJson<Void> delete(Integer id, Account account) {
        VehicleCarDeleteDTO vehicleCarDeleteDTO = new VehicleCarDeleteDTO();
        vehicleCarDeleteDTO.setId(id);

        DeleteVehicleCarBO deleteVehicleCarBO = vehicleCarSecondConverter.toDeleteVehicleCarBO(vehicleCarDeleteDTO, account.getId(), account.getName());

        //对删除进行授权
        VehicleCar vehicleCar = vehicleCarService.getById(id);
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(vehicleCar.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();
        IVehicleParkingLotCarAuthHandler vehicleParkingLotCarAuthHandler = applicationContext.getBean(VehicleParkingLotCarAuthEnum.toVehicleParkingLotCarAuthEnum(productId).getHandlerName(), IVehicleParkingLotCarAuthHandler.class);
        String carDataStr = JSONObject.toJSONString(deleteVehicleCarBO);
        vehicleParkingLotCarAuthHandler.deleteCar(vehicleIntegratedConfigBO,carDataStr);


        vehicleCarBusinessService.deleteCar(deleteVehicleCarBO);

        return ResultJson.successWithVoid();
    }

    @PostMapping("/car/update")
    public ResultJson<Void> update(@RequestBody VehicleCarUpdateDTO vehicleCarUpdateDTO, Account account) {
        UpdateVehicleCarBO updateVehicleCarBO = vehicleCarSecondConverter.toUpdateVehicleCarBO(vehicleCarUpdateDTO, account.getId(), account.getName());

        //对更新进行授权
        VehicleCar vehicleCar = vehicleCarService.getById(vehicleCarUpdateDTO.getId());
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromProjectId(vehicleCar.getProjectId());
        Integer productId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProductId();
        IVehicleParkingLotCarAuthHandler vehicleParkingLotCarAuthHandler = applicationContext.getBean(VehicleParkingLotCarAuthEnum.toVehicleParkingLotCarAuthEnum(productId).getHandlerName(), IVehicleParkingLotCarAuthHandler.class);
        String carDataStr = JSONObject.toJSONString(updateVehicleCarBO);
        vehicleParkingLotCarAuthHandler.updateCar(vehicleIntegratedConfigBO,carDataStr);

        vehicleCarBusinessService.updateCar(updateVehicleCarBO);

        return ResultJson.successWithVoid();
    }

    /**
     * 月卡续期
     */
    @PostMapping("/month-car/charge-renew")
    public void chargeMonthCar(HttpServletRequest request, HttpServletResponse response, Account account){
        vehicleCarBusinessService.chargeMonthCar(request, response, account);
    }
}
